import {SimpleGit} from 'simple-git'
import process from 'process'
import * as step from '@flow-step/step-toolkit'
import {deleteBranch} from './deleteBranch'

export async function deleteBranchWithFilter(git: SimpleGit,currentBranch: string,) {
    if (process.env.DELETE_MATCHED_BRANCH == 'true') {
        const filter = process.env.BRANCH_FILTER
        if (filter != undefined && filter != '' && new RegExp(filter).test(currentBranch)) {
            step.info(`Deleting branch ${currentBranch} which matches the filter ${filter}`)
            await deleteBranch(git, currentBranch)
        } else {
            step.info(`Branch ${currentBranch} does not match the filter ${process.env.BRANCH_FILTER}, skip deleting.`)
        }
    }else{
        step.info(`Deleting branch ${currentBranch} without filter`)
        await deleteBranch(git,currentBranch);
    }
}